package com.taptech.doufu.util;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.taptech.doufu.ui.activity.NewAlbumsActivity;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.activity.AnswerStarActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.EasyBrowseActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.activity.PersonalDynamicActivity;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;

import java.util.HashMap;

public class StartActivityUtil {
	/***
	 * 用户
	 * @param userBean
	 * @param mContext
	 */
	public static void startUserActivity(UserBean userBean,Context mContext){
		String userId = userBean.getUserId();
		startUserActivity(userId, mContext);
	}
	public static void startUserActivity(String userId,Context mContext){
		if (!userId.equals(AccountService.getInstance().getUserUid())) {
			UserDetailActivity.Companion.startActivity(mContext,userId);
		}
		
	}
	/**
	 * 专题点击
	 */
	public static void albumsOnclick(Context context,AlbumsBean bean){
		if (bean==null) {
			return;
		}
		int type=DiaobaoUtil.String2Int(bean.getType());
		Intent intent=new Intent(context, NewAlbumsActivity.class);
		intent.putExtra(Constant.URL, bean.getUrl());
		intent.putExtra(Constant.TYPE, type);
		intent.putExtra(Constant.NAME, bean.getName());
		intent.putExtra(Constant.RANK_FALG , bean.isRank_flag());
		context.startActivity(intent);
	}
	public static void albumsOnclick(Context context,AlbumsBean bean ,boolean rankFlag){
		if (bean==null) {
			return;
		}
		int type=DiaobaoUtil.String2Int(bean.getType());
		Intent intent=new Intent(context, NewAlbumsActivity.class);
		intent.putExtra(Constant.URL, bean.getUrl());
		intent.putExtra(Constant.TYPE, type);
		intent.putExtra(Constant.NAME, bean.getName());
		intent.putExtra(Constant.RANK_FALG , rankFlag);
		context.startActivity(intent);
	}
	/**
	 * 小说详情
	 */
	public static void novelOnclick(Context context,String id){
		NovelDetailsActivity.Companion.startActivity(context,id,Intent.FLAG_ACTIVITY_NEW_TASK);
	}
	
	public static void enterOtherPersonalCenter(Context context , String userId){
		if(AccountService.getInstance().isUserSelf(userId)){
			return;
		}
		UserDetailActivity.Companion.startActivity(context,userId,Intent.FLAG_ACTIVITY_NEW_TASK);
	}


	public static void bannerOnclick(Context mContext,DFHomeBannerBean bean) {
		if (bean == null) {
			return;
		}
		Intent intent = new Intent();
		switch (DiaobaoUtil.String2Int(bean.getObject_type()))
		{
			case Constant.OBJECT_TYPE_NOVEL://18小说
				StartActivityUtil.novelOnclick(mContext, bean.getId());
				break;

			case Constant.OBJECT_TYPE_ANSWER://腐化度测试
				if (AccountService.getInstance().isLogin())
				{
					intent.setClass(mContext, AnswerStarActivity.class);
					mContext.startActivity(intent);
				} else
				{
					AccountService.getInstance().jumpToLogin();
				}
				break;
			case Constant.OBJECT_TYPE_BROWSE:// 跳转到内部网页的类型
				String url = bean.getUrl();
				String title = bean.getTitle();
				intent.setClass(mContext, BrowseActivity.class);
				intent.putExtra(Constant.URL, url);
				intent.putExtra(Constant.TITLE, title);
				mContext.startActivity(intent);
				break;
			case Constant.OBJECT_TYPE_EASY_WEBVIEW:
				intent.setClass(mContext, EasyBrowseActivity.class);
				intent.putExtra(Constant.URL, bean.getUrl());
				intent.putExtra(Constant.TITLE, bean.getTitle());
				mContext.startActivity(intent);
				break;
			case 54:
				intent.setClass(mContext, EasyBrowseActivity.class);
				intent.putExtra(Constant.URL, bean.getUrl());
				intent.putExtra(Constant.TITLE, bean.getTitle());
				mContext.startActivity(intent);
				break;
			case Constant.OBJECT_TYPE_ALBUMS:
				AlbumsBean albumsBean = new AlbumsBean();
				albumsBean.setType(bean.getType());
				albumsBean.setName(bean.getTitle());
				albumsBean.setUrl(bean.getUrl());
				StartActivityUtil.albumsOnclick(mContext, albumsBean);
				break;
			case Constant.CONTENT_TYPE_TOPIC:
				switch (DiaobaoUtil.String2Int(bean.getTopic_type()))
				{
					case Constant.OBJECT_TYPE_NOVEL:
						StartActivityUtil.novelOnclick(mContext, bean.getId());
						break;

					case Constant.OBJECT_TYPE_ANSWER:
						if (AccountService.getInstance().isLogin())
						{
							intent.setClass(mContext, AnswerStarActivity.class);
							mContext.startActivity(intent);
						} else
						{
							AccountService.getInstance().jumpToLogin();
						}
						break;
					case Constant.OBJECT_TYPE_SWEEP:
						if (AccountService.getInstance().isLogin())
						{
							int type = DiaobaoUtil.String2Int(bean.getType());
							intent.setClass(mContext, NewAlbumsActivity.class);
							intent.putExtra(Constant.URL, bean.getUrl());
							intent.putExtra(Constant.TYPE, type);
							intent.putExtra(Constant.NAME, bean.getName());
							mContext.startActivity(intent);
						} else
						{
							AccountService.getInstance().jumpToLogin();
						}
						break;
					default:
						break;

				}
				break;
			case Constant.OBJECT_TYPE_NOTE:
			case Constant.OBJECT_TYPE_DRAW:
			case Constant.OBJECT_TYPE_COS:
				PersonalNoteService.enterPersonalNoteDesActivity(mContext, bean.getId(), bean.getObject_type());
				break;
			case Constant.OBJECT_TYPE_CP:
				CpService.enterCpDesActivity(mContext, bean.getId());
				break;
			case Constant.OBJECT_TYPE_CARTOON:
				CartoonServices.enterCartoonDes(mContext, bean.getId());
				break;
			default:
				String jumpUrl = bean.getUrl();
				if (TextUtils.isEmpty(jumpUrl)) {
					jumpUrl = bean.getBanner_link();
				}
				if(!TextUtils.isEmpty(jumpUrl) && jumpUrl.contains("/weex/")){
					if (jumpUrl.contains("vue/speakNovel/read/WWRead.js")){//对话小说特殊处理
						String[] params = jumpUrl.split("\\?");
						HashMap hashMap = new HashMap<>();
						String sUrl = params[0];
						if (params.length > 1) {
							String datas = params[1];
							String[] items = datas.split("&");
							for (String item:items) {
								String[] kv = item.split("=");
								if (kv.length > 1) {
									hashMap.put(kv[0], kv[1]);
								}
							}
						}
						String type = DiaoBaoSharedPreferences.getSharedPreferencesValueToString("themeType",WeMediaApplication.applicationContext,"");
						if (!TextUtils.isEmpty(type)) {
							hashMap.put("themeType", JSON.parse(type));
						}
						SimpleWeexActivity.startActivity(mContext,sUrl,hashMap);
					}else{  //
						SimpleWeexActivity.startActivity(mContext,jumpUrl);
					}
				}
				break;

		}
	}
}
